<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>参数说明 | 课程简介</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C01-CreateProject/StartDevelopmentServer.html" />
    
    
    <link rel="prev" href="../C01-CreateProject/HelloWorld.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="2.3"
        data-chapter-title="参数说明"
        data-filepath="C01-CreateProject/AboutParams.md"
        data-basepath=".."
        data-revision="Sat May 11 2019 20:06:35 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="AboutFlask.html">
            
                
                    <a href="../AboutFlask.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Flask介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C01-CreateProject/index.html">
            
                
                    <a href="../C01-CreateProject/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        工程搭建
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C01-CreateProject/EnvInstall.html">
            
                
                    <a href="../C01-CreateProject/EnvInstall.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        环境安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C01-CreateProject/HelloWorld.html">
            
                
                    <a href="../C01-CreateProject/HelloWorld.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        HelloWorld程序
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="2.3" data-path="C01-CreateProject/AboutParams.html">
            
                
                    <a href="../C01-CreateProject/AboutParams.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        参数说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="C01-CreateProject/StartDevelopmentServer.html">
            
                
                    <a href="../C01-CreateProject/StartDevelopmentServer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        开发服务器运行方式
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C02-RouterAndBlueprint/index.html">
            
                
                    <a href="../C02-RouterAndBlueprint/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        路由与蓝图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C02-RouterAndBlueprint/Router.html">
            
                
                    <a href="../C02-RouterAndBlueprint/Router.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        路由
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C02-RouterAndBlueprint/Blueprint.html">
            
                
                    <a href="../C02-RouterAndBlueprint/Blueprint.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        蓝图
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C03-RequestAndResponse/index.html">
            
                
                    <a href="../C03-RequestAndResponse/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        请求与响应
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C03-RequestAndResponse/HandleRequest.html">
            
                
                    <a href="../C03-RequestAndResponse/HandleRequest.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        处理请求
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C03-RequestAndResponse/HandleResponse.html">
            
                
                    <a href="../C03-RequestAndResponse/HandleResponse.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        处理响应
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C03-RequestAndResponse/CookieAndSession.html">
            
                
                    <a href="../C03-RequestAndResponse/CookieAndSession.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        Cookie与Session
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C04-HookAndContext/index.html">
            
                
                    <a href="../C04-HookAndContext/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        请求钩子与上下文
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C04-HookAndContext/AbortAndErrorhandler.html">
            
                
                    <a href="../C04-HookAndContext/AbortAndErrorhandler.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        异常处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C04-HookAndContext/Hook.html">
            
                
                    <a href="../C04-HookAndContext/Hook.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        请求钩子
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="C04-HookAndContext/Context.html">
            
                
                    <a href="../C04-HookAndContext/Context.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        上下文
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="C05-Flask-RESTful/index.html">
            
                
                    <a href="../C05-Flask-RESTful/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        Flask-RESTful
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="C05-Flask-RESTful/QuickStart.html">
            
                
                    <a href="../C05-Flask-RESTful/QuickStart.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        起步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="C05-Flask-RESTful/AboutView.html">
            
                
                    <a href="../C05-Flask-RESTful/AboutView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        关于视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="C05-Flask-RESTful/AboutRequest.html">
            
                
                    <a href="../C05-Flask-RESTful/AboutRequest.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        关于请求
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="C05-Flask-RESTful/AboutResponse.html">
            
                
                    <a href="../C05-Flask-RESTful/AboutResponse.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        关于响应
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >课程简介</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x53C2;&#x6570;&#x8BF4;&#x660E;">&#x53C2;&#x6570;&#x8BF4;&#x660E;</h1>
<ul>
<li>Flask&#x5BF9;&#x8C61;&#x7684;&#x521D;&#x59CB;&#x5316;&#x53C2;&#x6570;</li>
<li>&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x914D;&#x7F6E;&#x53C2;&#x6570;</li>
<li>app.run()&#x8FD0;&#x884C;&#x53C2;&#x6570;</li>
</ul>
<h2 id="1-flask&#x5BF9;&#x8C61;&#x521D;&#x59CB;&#x5316;&#x53C2;&#x6570;">1 Flask&#x5BF9;&#x8C61;&#x521D;&#x59CB;&#x5316;&#x53C2;&#x6570;</h2>
<p>Flask &#x7A0B;&#x5E8F;&#x5B9E;&#x4F8B;&#x5728;&#x521B;&#x5EFA;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x9700;&#x8981;&#x9ED8;&#x8BA4;&#x4F20;&#x5165;&#x5F53;&#x524D; Flask &#x7A0B;&#x5E8F;&#x6240;&#x6307;&#x5B9A;&#x7684;&#x5305;(&#x6A21;&#x5757;)&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x5C31;&#x6765;&#x8BE6;&#x7EC6;&#x67E5;&#x770B;&#x4E00;&#x4E0B; Flask &#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x5728;&#x521B;&#x5EFA;&#x7684;&#x65F6;&#x5019;&#x4E00;&#x4E9B;&#x9700;&#x8981;&#x6211;&#x4EEC;&#x5173;&#x6CE8;&#x7684;&#x53C2;&#x6570;&#xFF1A;</p>
<ul>
<li>import_name<ul>
<li>Flask&#x7A0B;&#x5E8F;&#x6240;&#x5728;&#x7684;&#x5305;(&#x6A21;&#x5757;)&#xFF0C;&#x4F20; <code>__name__</code> &#x5C31;&#x53EF;&#x4EE5;</li>
<li>&#x5176;&#x53EF;&#x4EE5;&#x51B3;&#x5B9A; Flask &#x5728;&#x8BBF;&#x95EE;&#x9759;&#x6001;&#x6587;&#x4EF6;&#x65F6;&#x67E5;&#x627E;&#x7684;&#x8DEF;&#x5F84;</li>
</ul>
</li>
<li>static_url_path<ul>
<li>&#x9759;&#x6001;&#x6587;&#x4EF6;&#x8BBF;&#x95EE;&#x8DEF;&#x5F84;&#xFF0C;&#x53EF;&#x4EE5;&#x4E0D;&#x4F20;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;&#xFF1A;<code>/ + static_folder</code></li>
</ul>
</li>
<li>static_folder<ul>
<li>&#x9759;&#x6001;&#x6587;&#x4EF6;&#x5B58;&#x50A8;&#x7684;&#x6587;&#x4EF6;&#x5939;&#xFF0C;&#x53EF;&#x4EE5;&#x4E0D;&#x4F20;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A; <code>static</code></li>
</ul>
</li>
<li>template_folder<ul>
<li>&#x6A21;&#x677F;&#x6587;&#x4EF6;&#x5B58;&#x50A8;&#x7684;&#x6587;&#x4EF6;&#x5939;&#xFF0C;&#x53EF;&#x4EE5;&#x4E0D;&#x4F20;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A; <code>templates</code></li>
</ul>
</li>
</ul>
<h4 id="&#x9ED8;&#x8BA4;&#x53C2;&#x6570;&#x60C5;&#x51B5;&#x4E0B;">&#x9ED8;&#x8BA4;&#x53C2;&#x6570;&#x60C5;&#x51B5;&#x4E0B;</h4>
<pre><code class="lang-python">app = Flask(__name__)
</code></pre>
<p>&#x6587;&#x4EF6;&#x76EE;&#x5F55;</p>
<pre><code class="lang-python">----
  |---static
  |     |--- <span class="hljs-number">1.</span>png
  |---helloworld.py
</code></pre>
<p>&#x8BBF;&#x95EE; <code>127.0.0.1:5000/static/1.png</code> &#x5C31;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x5230;&#x56FE;&#x7247;</p>
<h4 id="&#x4FEE;&#x6539;&#x53C2;&#x6570;&#x7684;&#x60C5;&#x51B5;&#x4E0B;">&#x4FEE;&#x6539;&#x53C2;&#x6570;&#x7684;&#x60C5;&#x51B5;&#x4E0B;</h4>
<pre><code class="lang-python">app = Flask(__name__, static_url_path=<span class="hljs-string">&apos;/url_path_param&apos;</span>, static_folder=<span class="hljs-string">&apos;folder_param&apos;</span>)
</code></pre>
<p>&#x6587;&#x4EF6;&#x76EE;&#x5F55;</p>
<pre><code class="lang-python">----
  |---folder_param     <span class="hljs-comment"># &#x6B64;&#x5904;&#x76EE;&#x5F55;&#x540D;&#x53D8;&#x5316;</span>
  |     |--- <span class="hljs-number">1.</span>png
  |---helloworld.py
</code></pre>
<p>&#x8BBF;&#x95EE;<code>127.0.0.1:5000/url_path_param/1.png</code>&#x624D;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x5230;&#x56FE;&#x7247;</p>
<h2 id="2-&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x914D;&#x7F6E;&#x53C2;&#x6570;">2 &#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x914D;&#x7F6E;&#x53C2;&#x6570;</h2>
<p>&#x5BF9;&#x4E8E;Flask&#x5BF9;&#x8C61;&#x521D;&#x59CB;&#x5316;&#x53C2;&#x6570;&#x4EC5;&#x4EC5;&#x8BBE;&#x7F6E;&#x7684;&#x662F;Flask&#x672C;&#x8EAB;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x6BD4;&#x5982;&#xFF1A;</p>
<ul>
<li>Flask&#x4ECE;&#x54EA;&#x91CC;&#x8BFB;&#x53D6;&#x9759;&#x6001;&#x6587;&#x4EF6;</li>
<li>Flask&#x4ECE;&#x54EA;&#x91CC;&#x8BFB;&#x53D6;&#x6A21;&#x677F;&#x6587;&#x4EF6;</li>
<li>...</li>
</ul>
<p>&#x7B49;&#x7B49;&#x3002;</p>
<p>&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x914D;&#x7F6E;&#x53C2;&#x6570;&#x8BBE;&#x7F6E;&#x7684;&#x662F;&#x4E00;&#x4E2A;Web&#x5E94;&#x7528;&#x5DE5;&#x7A0B;&#x7684;&#x76F8;&#x5173;&#x4FE1;&#x606F;&#xFF0C;&#x6BD4;&#x5982;&#xFF1A;</p>
<ul>
<li>&#x6570;&#x636E;&#x5E93;&#x7684;&#x8FDE;&#x63A5;&#x4FE1;&#x606F;</li>
<li>&#x65E5;&#x5FD7;&#x7684;&#x914D;&#x7F6E;&#x4FE1;&#x606F;</li>
<li>&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x914D;&#x7F6E;&#x4FE1;&#x606F;</li>
<li>...</li>
</ul>
<p>&#x7B49;&#x7B49;</p>
<h3 id="&#x4F5C;&#x7528;">&#x4F5C;&#x7528;</h3>
<p>&#x96C6;&#x4E2D;&#x7BA1;&#x7406;&#x9879;&#x76EE;&#x7684;&#x6240;&#x6709;&#x914D;&#x7F6E;&#x4FE1;&#x606F;</p>
<h3 id="&#x4F7F;&#x7528;&#x65B9;&#x5F0F;">&#x4F7F;&#x7528;&#x65B9;&#x5F0F;</h3>
<p>Django&#x5C06;&#x6240;&#x6709;&#x914D;&#x7F6E;&#x4FE1;&#x606F;&#x90FD;&#x653E;&#x5230;&#x4E86;settings.py&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x800C;Flask&#x5219;&#x4E0D;&#x540C;&#x3002;</p>
<p><strong>Flask&#x5C06;&#x914D;&#x7F6E;&#x4FE1;&#x606F;&#x4FDD;&#x5B58;&#x5230;&#x4E86;<code>app.config</code>&#x5C5E;&#x6027;&#x4E2D;&#xFF0C;&#x8BE5;&#x5C5E;&#x6027;&#x53EF;&#x4EE5;&#x6309;&#x7167;&#x5B57;&#x5178;&#x7C7B;&#x578B;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#x3002;</strong></p>
<h4 id="&#x8BFB;&#x53D6;">&#x8BFB;&#x53D6;</h4>
<ul>
<li><code>app.config.get(name)</code></li>
<li><code>app.config[name]</code></li>
</ul>
<h4 id="&#x8BBE;&#x7F6E;">&#x8BBE;&#x7F6E;</h4>
<p>&#x4E3B;&#x8981;&#x4F7F;&#x7528;&#x4EE5;&#x4E0B;&#x4E09;&#x79CD;&#x65B9;&#x5F0F;&#xFF1A;</p>
<ul>
<li><p><strong>&#x4ECE;&#x914D;&#x7F6E;&#x5BF9;&#x8C61;&#x4E2D;&#x52A0;&#x8F7D;</strong></p>
<p><code>app.config.from_object(&#x914D;&#x7F6E;&#x5BF9;&#x8C61;)</code></p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DefaultConfig</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x9ED8;&#x8BA4;&#x914D;&#x7F6E;&quot;&quot;&quot;</span>
    SECRET_KEY = <span class="hljs-string">&apos;TPmi4aLWRbyVq8zu9v82dWYW1&apos;</span>

app = Flask(__name__)

app.config.from_object(DefaultConfig)

<span class="hljs-decorator">@app.route(&quot;/&quot;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">()</span>:</span>
    print(app.config[<span class="hljs-string">&apos;SECRET_KEY&apos;</span>])
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;hello world&quot;</span>
</code></pre>
<p>&#x5E94;&#x7528;&#x573A;&#x666F;&#xFF1A;</p>
<p>&#x4F5C;&#x4E3A;&#x9ED8;&#x8BA4;&#x914D;&#x7F6E;&#x5199;&#x5728;&#x7A0B;&#x5E8F;&#x4EE3;&#x7801;&#x4E2D;</p>
<p>&#x53EF;&#x4EE5;&#x7EE7;&#x627F;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DevelopmentConfig</span><span class="hljs-params">(DefaultConfig)</span>:</span>
    DEBUG=<span class="hljs-keyword">True</span>
</code></pre>
</li>
<li><p><strong>&#x4ECE;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4E2D;&#x52A0;&#x8F7D;</strong></p>
<p><code>app.config.from_pyfile(&#x914D;&#x7F6E;&#x6587;&#x4EF6;)</code></p>
<p>&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x914D;&#x7F6E;&#x6587;&#x4EF6;setting.py</p>
<pre><code class="lang-python">SECRET_KEY = <span class="hljs-string">&apos;TPmi4aLWRbyVq8zu9v82dWYW1&apos;</span>
</code></pre>
<p>&#x5728;Flask&#x7A0B;&#x5E8F;&#x6587;&#x4EF6;&#x4E2D;</p>
<pre><code class="lang-python">app = Flask(__name__)

app.config.from_pyfile(<span class="hljs-string">&apos;setting.py&apos;</span>)

<span class="hljs-decorator">@app.route(&quot;/&quot;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">()</span>:</span>
    print(app.config[<span class="hljs-string">&apos;SECRET_KEY&apos;</span>])
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;hello world&quot;</span>
</code></pre>
<p>&#x5E94;&#x7528;&#x573A;&#x666F;&#xFF1A;</p>
<p>&#x5728;&#x9879;&#x76EE;&#x4E2D;&#x4F7F;&#x7528;&#x56FA;&#x5B9A;&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;</p>
</li>
<li><p><strong>&#x4ECE;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x4E2D;&#x52A0;&#x8F7D;</strong></p>
<blockquote>
<p><strong>&#x73AF;&#x5883;&#x53D8;&#x91CF;</strong>&#xFF08;environment variables&#xFF09;&#x4E00;&#x822C;&#x662F;&#x6307;&#x5728;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x4E2D;&#x7528;&#x6765;&#x6307;&#x5B9A;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x8FD0;&#x884C;<strong>&#x73AF;&#x5883;</strong>&#x7684;&#x4E00;&#x4E9B;&#x53C2;&#x6570;&#xFF0C;&#x5982;&#xFF1A;&#x4E34;&#x65F6;&#x6587;&#x4EF6;&#x5939;&#x4F4D;&#x7F6E;&#x548C;&#x7CFB;&#x7EDF;&#x6587;&#x4EF6;&#x5939;&#x4F4D;&#x7F6E;&#x7B49;&#x3002; <strong>&#x73AF;&#x5883;&#x53D8;&#x91CF;</strong>&#x662F;&#x5728;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x4E2D;&#x4E00;&#x4E2A;&#x5177;&#x6709;&#x7279;&#x5B9A;&#x540D;&#x5B57;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x5B83;&#x5305;&#x542B;&#x4E86;&#x4E00;&#x4E2A;&#x6216;&#x8005;&#x591A;&#x4E2A;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x6240;&#x5C06;&#x4F7F;&#x7528;&#x5230;&#x7684;&#x4FE1;&#x606F;&#x3002;</p>
</blockquote>
<p>&#x901A;&#x4FD7;&#x7684;&#x7406;&#x89E3;&#xFF0C;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x5C31;&#x662F;&#x6211;&#x4EEC;&#x8BBE;&#x7F6E;&#x5728;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x4E2D;&#xFF0C;&#x7531;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x4EE3;&#x4E3A;&#x4FDD;&#x5B58;&#x7684;&#x53D8;&#x91CF;&#x503C;</p>
<p>&#x5728;Linux&#x7CFB;&#x7EDF;&#x4E2D;&#x8BBE;&#x7F6E;&#x548C;&#x8BFB;&#x53D6;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x7684;&#x65B9;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-shell">export &#x53D8;&#x91CF;&#x540D;=&#x53D8;&#x91CF;&#x503C;  # &#x8BBE;&#x7F6E;
echo $&#x53D8;&#x91CF;&#x540D;  # &#x8BFB;&#x53D6;

# &#x4F8B;&#x5982;
export ITCAST=python
echo $ITCAST
</code></pre>
<p><strong>Flask&#x4F7F;&#x7528;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x52A0;&#x8F7D;&#x914D;&#x7F6E;&#x7684;&#x672C;&#x8D28;&#x662F;&#x901A;&#x8FC7;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x503C;&#x627E;&#x5230;&#x914D;&#x7F6E;&#x6587;&#x4EF6;</strong>&#xFF0C;&#x518D;&#x8BFB;&#x53D6;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x5176;&#x4F7F;&#x7528;&#x65B9;&#x5F0F;&#x4E3A;</p>
<p><code>app.config.from_envvar(&apos;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x540D;&apos;)</code></p>
<p>&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x7684;&#x503C;&#x4E3A;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x7684;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;</p>
<p>&#x5148;&#x5728;&#x7EC8;&#x7AEF;&#x4E2D;&#x6267;&#x884C;&#x5982;&#x4E0B;&#x547D;&#x4EE4;</p>
<pre><code class="lang-shell">export PROJECT_SETTING=&apos;~/setting.py&apos;
</code></pre>
<p>&#x518D;&#x8FD0;&#x884C;&#x5982;&#x4E0B;&#x4EE3;&#x7801;</p>
<pre><code class="lang-python">app = Flask(__name__)

app.config.from_envvar(<span class="hljs-string">&apos;PROJECT_SETTING&apos;</span>, silent=<span class="hljs-keyword">True</span>)

<span class="hljs-decorator">@app.route(&quot;/&quot;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">()</span>:</span>
    print(app.config[<span class="hljs-string">&apos;SECRET_KEY&apos;</span>])
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;hello world&quot;</span>
</code></pre>
<p>&#x5173;&#x4E8E;<code>silent</code>&#x7684;&#x8BF4;&#x660E;&#xFF1A;</p>
<p>&#x8868;&#x793A;&#x7CFB;&#x7EDF;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x4E2D;&#x6CA1;&#x6709;&#x8BBE;&#x7F6E;&#x76F8;&#x5E94;&#x503C;&#x65F6;&#x662F;&#x5426;&#x629B;&#x51FA;&#x5F02;&#x5E38;</p>
<ul>
<li>False &#x8868;&#x793A;&#x4E0D;&#x5B89;&#x9759;&#x7684;&#x5904;&#x7406;&#xFF0C;&#x6CA1;&#x6709;&#x503C;&#x65F6;&#x62A5;&#x9519;&#x901A;&#x77E5;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;False</li>
<li>True &#x8868;&#x793A;&#x5B89;&#x9759;&#x7684;&#x5904;&#x7406;&#xFF0C;&#x5373;&#x65F6;&#x6CA1;&#x6709;&#x503C;&#x4E5F;&#x8BA9;Flask&#x6B63;&#x5E38;&#x7684;&#x8FD0;&#x884C;&#x4E0B;&#x53BB;</li>
</ul>
<h5 id="pycharm&#x8FD0;&#x884C;&#x65F6;&#x8BBE;&#x7F6E;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x7684;&#x65B9;&#x5F0F;">Pycharm&#x8FD0;&#x884C;&#x65F6;&#x8BBE;&#x7F6E;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x7684;&#x65B9;&#x5F0F;</h5>
<p><img src="../images/Pycharm&#x8BBE;&#x7F6E;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x7B2C;&#x4E00;&#x6B65;.png" alt=""></p>
<p><img src="../images/Pycharm&#x8BBE;&#x7F6E;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x7B2C;&#x4E8C;&#x6B65;.png" alt=""></p>
</li>
</ul>
<p>&#x200B;        &#x5E94;&#x7528;&#x573A;&#x666F;&#xFF1A;</p>
<p>&#x200B;        &#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x7684;&#x5730;&#x5740;&#x4E0D;&#x56FA;&#x5B9A;&#xFF1B;</p>
<p>&#x200B;        &#x5728;&#x4EE3;&#x7801;&#x4E2D;&#x4E0D;&#x60F3;&#x66B4;&#x9732;&#x771F;&#x5B9E;&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x5730;&#x5740;&#xFF0C;&#x53EA;&#x5728;&#x8FD0;&#x884C;&#x4EE3;&#x7801;&#x7684;&#x670D;&#x52A1;&#x5668;&#x4E0A;&#x624D;&#x6709;&#x771F;&#x5B9E;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x7684;&#x4FE1;&#x606F;&#x3002;</p>
<h3 id="&#x9879;&#x76EE;&#x4E2D;&#x7684;&#x5E38;&#x7528;&#x65B9;&#x5F0F;">&#x9879;&#x76EE;&#x4E2D;&#x7684;&#x5E38;&#x7528;&#x65B9;&#x5F0F;</h3>
<p>&#x4F7F;&#x7528;&#x5DE5;&#x5382;&#x6A21;&#x5F0F;&#x521B;&#x5EFA;Flask app&#xFF0C;&#x5E76;&#x7ED3;&#x5408;&#x4F7F;&#x7528;&#x914D;&#x7F6E;&#x5BF9;&#x8C61;&#x4E0E;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x52A0;&#x8F7D;&#x914D;&#x7F6E;</p>
<ul>
<li>&#x4F7F;&#x7528;&#x914D;&#x7F6E;&#x5BF9;&#x8C61;&#x52A0;&#x8F7D;&#x9ED8;&#x8BA4;&#x914D;&#x7F6E;</li>
<li>&#x4F7F;&#x7528;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x52A0;&#x8F7D;&#x4E0D;&#x60F3;&#x51FA;&#x73B0;&#x5728;&#x4EE3;&#x7801;&#x4E2D;&#x7684;&#x654F;&#x611F;&#x914D;&#x7F6E;&#x4FE1;&#x606F;</li>
</ul>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">create_flask_app</span><span class="hljs-params">(config)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x521B;&#x5EFA;Flask&#x5E94;&#x7528;
    :param config: &#x914D;&#x7F6E;&#x5BF9;&#x8C61;
    :return: Flask&#x5E94;&#x7528;
    &quot;&quot;&quot;</span>
    app = Flask(__name__)
    app.config.from_object(config)

    <span class="hljs-comment"># &#x4ECE;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x6307;&#x5411;&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4E2D;&#x8BFB;&#x53D6;&#x7684;&#x914D;&#x7F6E;&#x4FE1;&#x606F;&#x4F1A;&#x8986;&#x76D6;&#x6389;&#x4ECE;&#x914D;&#x7F6E;&#x5BF9;&#x8C61;&#x4E2D;&#x52A0;&#x8F7D;&#x7684;&#x540C;&#x540D;&#x53C2;&#x6570;</span>
    app.config.from_envvar(<span class="hljs-string">&quot;PROJECT_SETTING&quot;</span>, silent=<span class="hljs-keyword">True</span>)
    <span class="hljs-keyword">return</span> app

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DefaultConfig</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x9ED8;&#x8BA4;&#x914D;&#x7F6E;&quot;&quot;&quot;</span>
    SECRET_KEY = <span class="hljs-string">&apos;itcast1&apos;</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DevelopmentConfig</span><span class="hljs-params">(DefaultConfig)</span>:</span>
    DEBUG=<span class="hljs-keyword">True</span>

<span class="hljs-comment"># app = create_flask_app(DefaultConfig)</span>
app = create_flask_app(DevelopmentConfig)

<span class="hljs-decorator">@app.route(&quot;/&quot;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">()</span>:</span>
    print(app.config[<span class="hljs-string">&apos;SECRET_KEY&apos;</span>])
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;hello world&quot;</span>
</code></pre>
<h2 id="3-apprun-&#x53C2;&#x6570;">3 app.run &#x53C2;&#x6570;</h2>
<p>&#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x8FD0;&#x884C;&#x7684;&#x4E3B;&#x673A;IP&#x5730;&#x5740;&#xFF0C;&#x7AEF;&#x53E3;&#xFF0C;&#x662F;&#x5426;&#x5F00;&#x542F;&#x8C03;&#x8BD5;&#x6A21;&#x5F0F;</p>
<pre><code class="lang-python">app.run(host=<span class="hljs-string">&quot;0.0.0.0&quot;</span>, port=<span class="hljs-number">5000</span>, debug = <span class="hljs-keyword">True</span>)
</code></pre>
<p>&#x5173;&#x4E8E;DEBUG&#x8C03;&#x8BD5;&#x6A21;&#x5F0F;</p>
<ol>
<li>&#x7A0B;&#x5E8F;&#x4EE3;&#x7801;&#x4FEE;&#x6539;&#x540E;&#x53EF;&#x4EE5;&#x81EA;&#x52A8;&#x91CD;&#x542F;&#x670D;&#x52A1;&#x5668;</li>
<li>&#x5728;&#x670D;&#x52A1;&#x5668;&#x51FA;&#x73B0;&#x76F8;&#x5173;&#x9519;&#x8BEF;&#x7684;&#x65F6;&#x5019;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5C06;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#x8FD4;&#x56DE;&#x5230;&#x524D;&#x7AEF;&#x8FDB;&#x884C;&#x5C55;&#x793A;</li>
</ol>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C01-CreateProject/HelloWorld.html" class="navigation navigation-prev " aria-label="Previous page: HelloWorld程序"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C01-CreateProject/StartDevelopmentServer.html" class="navigation navigation-next " aria-label="Next page: 开发服务器运行方式"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
